package name.zxw.rabbitmq.basic;


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @author zhangxinwang
 * @date 2022/6/17 13:45
 */
public class Task {

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setUsername("guest");
        factory.setPassword("guest");

        Channel channel = factory.newConnection().createChannel();

        channel.confirmSelect();

        channel.queueDeclare("test", true, false, false, null);

        Scanner scanner = new Scanner(System.in);
        /*while (scanner.hasNext()) {
            String message = scanner.next();
            channel.basicPublish("", "test", MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes(StandardCharsets.UTF_8));
            System.out.println("发送信息:" + message);
        }*/
        for (int i = 0; i < 500; i++) {
            TimeUnit.MILLISECONDS.sleep(200);
            channel.basicPublish("", "test", MessageProperties.PERSISTENT_TEXT_PLAIN, new String("" + i).getBytes(StandardCharsets.UTF_8));
            System.out.println("发送信息:" + i);
        }
        System.out.println("发送完成");

    }


}
